
#include "stdafx.h"
#include <allegro5/allegro_primitives.h>
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
#include "BaseComponent.h"

BaseComponent::BaseComponent(std::string modelName) :
	_modelName( modelName ), _bitmap( 0 ) {
  // Bouml preserved body begin 0001F47B
	_angle = 0.0;
  // Bouml preserved body end 0001F47B
}

BaseComponent::~BaseComponent(void ){
  // Bouml preserved body begin 0001F4FB
  // Bouml preserved body end 0001F4FB
}

//Loads figure from array of points.
//Array should hold numPoints * 2 floats.
void BaseComponent::LoadFigure(int numPoints, const float * listPoints) {
  // Bouml preserved body begin 0002347B
	_figure.clear();
	_figure.resize( numPoints );
	for (int i = 0; i < numPoints; i++ ) {
		_figure[i].x = listPoints[ i * 2];
		_figure[i].y = listPoints[ i * 2 + 1];
	}
  // Bouml preserved body end 0002347B
}

void BaseComponent::LoadBitmap( std::string file ) {
#ifdef __APPLE__
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    
    char cPath[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)cPath, PATH_MAX))
    {
        // error!
    }
    CFRelease(resourcesURL);

    std::string path(cPath);
    path += "/";
#else
	std::string path("");
#endif
	
   
    file += ".bmp";
    path += file;

    _bitmap = 0;
    _bitmap = al_load_bitmap( path.c_str() );
}

void BaseComponent::SetAngle(float angle) {
  // Bouml preserved body begin 00024F7B
	_angle = angle;
  // Bouml preserved body end 00024F7B
}

//Draws object in given position

void BaseComponent::DrawAt(const BaseComponent::point & position) {
  // Bouml preserved body begin 00024FFB
	ALLEGRO_TRANSFORM current, toUse;

	al_copy_transform( &current, al_get_current_transform() );
	al_identity_transform( &toUse );

	if ( _bitmap ) {
		al_translate_transform( &toUse, _bitmapCenter.x, _bitmapCenter.y );
		al_rotate_transform( &toUse, _angle );
		al_translate_transform( &toUse, position.x, position.y );
		al_compose_transform( &toUse, &current );
		al_use_transform( &toUse );
        al_draw_bitmap( _bitmap, .0, .0, 0 );
    } else {
		al_rotate_transform( &toUse, _angle );
		al_translate_transform( &toUse, position.x, position.y );
		al_compose_transform( &toUse, &current );
		al_use_transform( &toUse );
        if ( _figure.size() > 1 ) {
            for ( int i = 0; i < _figure.size() - 1; i++ ) {
                al_draw_line(
                             _figure[ i].x,
                             _figure[ i].y,
                             _figure[ i+1].x,
                             _figure[ i+1].y,
                             al_map_rgb( 255, 255, 255 ), 1 );
            }
            al_draw_line(
                         _figure[ 0].x,
                         _figure[ 0].y,
                         _figure[ _figure.size()-1].x,
                         _figure[ _figure.size()-1].y,
                         al_map_rgb( 255, 255, 255 ), 1 );
        }
    }
    

	al_use_transform( &current );
  // Bouml preserved body end 00024FFB
}

